package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.R;
import com.gxa.ssm340.common.Render;
import com.gxa.ssm340.entity.dto.UserPageDTO;
import com.gxa.ssm340.entity.pojo.Points;
import com.gxa.ssm340.entity.vo.UsersAndPointsCountVO;
import com.gxa.ssm340.entity.vo.UsersAndPointsVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.service.UserService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 会员接口
 * @author 赵文川
 * @Description: $
 * @date 2023/10/23 18:33
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getList")
    public Render getUsersAndPoints(UserPageDTO dto) throws BizException {
        // 设置每页显示条数为10
        dto.setLimit(10);
        UsersAndPointsCountVO usersAndPoints = userService.getUsersAndPoints(dto);
        return Render.success(usersAndPoints.getUsersAndPointsVOS(),usersAndPoints.getCount());
    }

    @GetMapping("/pointsGetList1")
    public R getPointsList(UserPageDTO dto) throws BizException {
        // 设置每页显示条数为10
        dto.setLimit(10);
        List<Points> pointDetail = userService.getPointDetail(dto,"in");
        return R.success(pointDetail);
    }

    @GetMapping("/pointsExchangeList1")
    public R getExchangeList(UserPageDTO dto) throws BizException {
        // 设置每页显示条数为10
        dto.setLimit(10);
        List<Points> pointDetail = userService.getPointDetail(dto,"out");
        return R.success(pointDetail);
    }

    @GetMapping("/exportList")
    public void exportCpnList(HttpServletResponse response) throws BizException, IOException {
        // 创建一个工作簿对象
        Workbook workbook = new XSSFWorkbook();
        UserPageDTO dto = new UserPageDTO();
        dto.setPage(1);
        dto.setLimit(500);
        Render r = this.getUsersAndPoints(dto);
        List<UsersAndPointsVO> data = (List<UsersAndPointsVO>) r.getData();
        // 创建一个工作表对象
        Sheet sheet = workbook.createSheet("points");
        Row title = sheet.createRow(0);
        int cell = 0;
        title.createCell(cell++).setCellValue("用户编号");
        title.createCell(cell++).setCellValue("昵称");
        title.createCell(cell++).setCellValue("手机号");
        title.createCell(cell++).setCellValue("总积分");
        // 创建行和单元格，并将数据写入单元格中
        int rowIndex = 0;
        for (UsersAndPointsVO item : data) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            row.createCell(cellIndex++).setCellValue(item.getUserId());
            row.createCell(cellIndex++).setCellValue(item.getName());
            row.createCell(cellIndex++).setCellValue(item.getPhone());
            row.createCell(cellIndex++).setCellValue(item.getTotalPoint());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (item.getRegTime()!=null) {
                String format = simpleDateFormat.format(item.getRegTime());
                row.createCell(cellIndex++).setCellValue(format);
            }
        }
        // 自动调整所有列的单元格大小
        for (int i = 0; i < data.get(0).getClass().getDeclaredFields().length; i++) {
            sheet.autoSizeColumn(i);
        }
        // 设置响应头信息，告诉浏览器响应的是一个Excel文件
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=\"UserPoints.xlsx\"");
        // 将Excel文件写入响应输出流中，返回给前端
        workbook.write(response.getOutputStream());
    }

    @GetMapping("/exportPointsList")
    public void exportPointsList(UserPageDTO dto, HttpServletResponse response) throws BizException, IOException {
        // 创建一个工作簿对象
        Workbook workbook = new XSSFWorkbook();
        // dto.setPage(1);
        // dto.setLimit(500);
        List<Points> pointDetail = userService.getPointDetail(dto);
        // 创建一个工作表对象
        Sheet sheet = workbook.createSheet("points");
        Row title = sheet.createRow(0);
        int cell = 0;
        title.createCell(cell++).setCellValue("积分编号");
        title.createCell(cell++).setCellValue("积分");
        title.createCell(cell++).setCellValue("手机号");
        title.createCell(cell++).setCellValue("图片");
        title.createCell(cell++).setCellValue("商品数量");
        title.createCell(cell++).setCellValue("商品名称");
        title.createCell(cell++).setCellValue("录入时间");
        // 创建行和单元格，并将数据写入单元格中
        int rowIndex = 1;
        for (Points item : pointDetail) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            row.createCell(cellIndex++).setCellValue(item.getPointsId());
            row.createCell(cellIndex++).setCellValue(item.getPoints());
            row.createCell(cellIndex++).setCellValue(item.getPhone());
            row.createCell(cellIndex++).setCellValue(item.getImg());
            row.createCell(cellIndex++).setCellValue(item.getNum());
            row.createCell(cellIndex++).setCellValue(item.getBrandName());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (item.getEntryTime()!=null) {
                String format = simpleDateFormat.format(item.getEntryTime());
                row.createCell(cellIndex++).setCellValue(format);
            }
        }
        // 自动调整所有列的单元格大小
        for (int i = 0; i < pointDetail.get(0).getClass().getDeclaredFields().length; i++) {
            sheet.autoSizeColumn(i);
        }
        // 设置响应头信息，告诉浏览器响应的是一个Excel文件
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=\"UserPointsDetails.xlsx\"");
        // 将Excel文件写入响应输出流中，返回给前端
        workbook.write(response.getOutputStream());
    }

}
